package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQueryObject qo) {

        // 创建拼接对象
        Query query = new Query();

        if(StringUtils.hasLength(qo.getStrategyId())) {
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }

        // 使用PageRequest.of,传入当前页 - 1 (记得-1, 因为当前页从0开始), 页面显示条数, 排序规则, 排序的列
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        return DBHelper.query(template, qo, query, pageable, StrategyComment.class);
    }

    @Override
    public void addComment(StrategyComment strategyComment, UserInfo userInfo) {
        // 需要维护: -用户头像 -评论级别为1 -createTime创建时间 -nickname用户名, -用户id
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setHeadImgUrl(userInfo.getHeadImgUrl());
        strategyComment.setLevel(1);
        strategyComment.setCreateTime(new Date());
        strategyComment.setNickname(userInfo.getNickname());
        this.save(strategyComment);
    }

    @Override
    public void commentThumb(String cid, UserInfo userInfo) {
        // 拿到commentId -- 拿出comment
        StrategyComment comment = this.get(cid);
        List<String> thumbuplist = comment.getThumbuplist();
        // 判断点赞列表是否为为空
        if (!thumbuplist.isEmpty()){
            // 如果点赞列表中有用户的Id, 那么视为取消赞
            if (thumbuplist.contains(userInfo.getId())){
                // 移出当前用户的id
                thumbuplist.remove(userInfo.getId());
                // -1
                comment.setThumbupnum(comment.getThumbupnum() - 1);
            } else {
                // 如果没有该用户的Id, 那么视为点赞
                thumbuplist.add(userInfo.getId());
                // +1
                comment.setThumbupnum(comment.getThumbupnum() + 1);
            }
        } else {
            thumbuplist.add(userInfo.getId());
            comment.setThumbupnum(1);
        }
        this.update(comment);
    }
}
